package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.InsertUserDto;
import com.heima.dto.UserResetDto;
import com.heima.dto.UserStatusDto;
import com.heima.dto.pagedto.UserPageDto;
import com.heima.result.ListRoleResult;
import com.heima.result.Result;
import com.heima.service.UserService;
import com.heima.vo.*;
import io.swagger.annotations.Api;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "角色模块")
public class UserController {

    private final UserService userService;

    @Autowired
    private HttpServletResponse httpServletResponse;

    /**
     * 获取用户初始密码
     * @return
     */
    @GetMapping("/system/config/configKey/sys.user.initPassword")
    public UserInitPassword getInitPassword(){
        log.info("获取初始密码");
        return new UserInitPassword();
    }

    /**
     * 用户分页查询
     */
    @GetMapping("/system/user/list")
    @OperationFill
    public PageResult queryUser(UserPageDto dto,
                                @RequestParam(value = "params[beginTime]",required = false) String begin,
                                @RequestParam(value = "params[endTime]",required = false) String end){
        dto.setBeginTime(begin);
        dto.setEndTime(end);
        log.info("用户分页查询");
        PageResult vo = userService.queryUser(dto);
        return vo;
    }

    /**
     * 新增用户
     */
    @PostMapping("/system/user")
    @OperationFill
    public ResultVo insertUser(InsertUserDto dto){
        log.info("新增用户");
        userService.insertUser(dto);
        return new ResultVo();
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/system/user/changeStatus")
    @OperationFill
    public ResultVo updateStatus(@RequestBody UserStatusDto dto){
        log.info("修改用户状态:{}",dto);
        userService.updateStatus(dto);
        return new ResultVo();
    }

    /**
     * 根据id查询用户的信息
     */
    @GetMapping("/system/user/{id}")
    @OperationFill
    public UserVo findUser(@PathVariable Integer id){
        log.info("根据id查询用户信息：{}",id);
        UserVo vo = userService.findUserById(id);
        return vo;
    }

    /**
     * 重置用户密码
     */
    @PutMapping("/system/user/resetPwd")
    @OperationFill
    public ResultVo resetPassword(@RequestBody UserResetDto dto){
        log.info("重置用户密码");
        userService.resetPassword(dto);
        return new ResultVo();
    }

    /**
     * 获取用户角色
     */
    @GetMapping("system/user/")
    public ListRoleResult queryRoles(){
        log.info("获取用户角色");
        ListRoleResult listRole =userService.listselect();
        return listRole;
    }

    @GetMapping("/system/user/listselect")
    public Result queryDeptUser(Integer deptId) {
        log.info("获取部门id {}", deptId);
        List<UserInfoDeptVo> userInfoDeptVoList =  userService.queryDeptUser(deptId);
        return Result.success(userInfoDeptVoList);
    }
    /**
     * 删除用户
     */
    @DeleteMapping("/system/user/{id}")
    public ResultVo deleteUser(@PathVariable Integer id){
        log.info("删除用户");
        userService.deleteUser(id);
        return  new ResultVo();
    }

    /**
     * 修改用户
     */
//    @PostMapping("/system/user")
//    public ResultVo updateUser(@RequestBody InsertUserDto dto){
//        log.info("修改用户");
//        userService.updateUser(dto);
//        return new ResultVo();
//    }

    public String convertRoleIdsToString(List<Integer> roleIds) {
        if (roleIds == null || roleIds.isEmpty()) {
            return null;
        }
        return String.join(",", roleIds.stream().map(Object::toString).collect(Collectors.toList()));
    }

    public List<Integer> convertStringToRoleIds(String roleIdsString) {
        if (roleIdsString == null || roleIdsString.isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.stream(roleIdsString.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
    }


}
